package Interview.didi;

import java.util.HashMap;

/**
 * 合并排序数组
 * @author leiwenkang
 * @date 2020/4/7
 */
public class Test30合并排序数组 {
    public static void main(String[] args) {
        int[] arr1 = {1, 3, 6, 9};
        int[] arr2 = {2, 4, 7, 8};
        int[] result = mergeSortArray(arr1, arr2);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }
        new HashMap<>();

    }

    public static int[] mergeSortArray(int[] array1, int[] array2) {
        if (array1 == null) {
            return array2;
        }
        if (array2 == null) {
            return array1;
        }

        int[] result = new int[array1.length + array2.length];
        int index = 0;
        int index1 = 0;
        int index2 = 0;

        while (index1 < array1.length && index2 < array2.length) {
            if (array1[index1] <= array2[index2]) {
                result[index] = array1[index1];
                index1++;
            } else {
                result[index] = array2[index2];
                index2++;
            }
            index++;
        }
        while (index1 < array1.length) {
            result[index] = array1[index1];
            index1++;
            index++;
        }

        while (index2 < array2.length) {
            result[index] = array2[index2];
            index2++;
            index++;
        }
        return result;
    }

}
